% fpspoint = 100;
% look_area = 0.015;
% look_up = 0.02;
% look_down = 0.0005;
% look_times = 30;
% 
% pc = sortrows(points,5,'descend');
% ind = find(pc(:,5),1,'last');
% linepoint = pc(1:ind,:);
%save('linepoint.mat','linepoint');
load('linepoint.mat')
ind = 9873;
mat = zeros(10000,10000);
for i = 1:ind
   mat(floor(linepoint(i,1)*10000)+1,floor(linepoint(i,2)*10000)+1) = 1; 
end
%plot(mat);
figure, imshow(mat), hold on
[H,T,R]=hough(mat);
% imshow(imadjust(rescale(H)),'XData',T,'YData',R,...
%       'InitialMagnification','fit');
% houf peak
P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
plot(T(P(:,2)),R(P(:,1)),'s','color','white');
%draw line
lines = houghlines(mat,T,R,P,'FillGap',5,'MinLength',30);
figure, imshow(mat), hold on
max_len = 0;
for k = 1:length(lines)
   x = lines(k).point1(1)-lines(k).point2(1);
   y = lines(k).point1(2)-lines(k).point2(2);
   m = y/x;
   b = lines(k).point1(2)-m*lines(k).point1(1);
   xx = 1:10000;
   yy = m*xx+b;
   plot(xx,yy)
%    % Plot beginnings and ends of lines
%    plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
%    plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

   % Determine the endpoints of the longest line segment
%    len = norm(lines(k).point1 - lines(k).point2);
%    if ( len > max_len)
%       max_len = len;
%       xy_long = xy;
%    end
end